package com.aaa.javaweb.servlet;


import com.aaa.javaweb.dao.RoleDao;
import com.aaa.javaweb.dao.impl.RoleDaoImpl;
import com.aaa.javaweb.entity.Role;
import com.aaa.javaweb.until.LayTable;
import com.aaa.javaweb.until.TransBean;
import com.alibaba.fastjson.JSON;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/queryAllRole")
public class RoleController extends HttpServlet {
    RoleDao roleDao=new RoleDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        if(method!=null){
            switch (method){
                case "del":
                    del(req,resp);
                    break;
                case "add":
                    add(req,resp);
                    break;
                case "upd":
                    upd(req,resp);
                    break;
                case "queryid":
                    queryid(req,resp);
                    break;
                case "updQx":
                    updid(req,resp);
                    break;
            }
        }else{
            List<Map> maps=roleDao.queryAll();
            Map m = new HashMap();
            m.put("code",0);
            m.put("msg","");
            m.put("count",maps.size());
            m.put("data",maps);
            String map= JSON.toJSONString(m);
            PrintWriter out = resp.getWriter();
            out.print(map);
            out.flush();
            out.close();
        }
    }
    //修改权限
    private void updid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String rid = req.getParameter("rid");
        String mid = req.getParameter("mid");
        int i=roleDao.del(rid);
        String[] split = mid.split(",");
        for (String s : split) {
            int j=roleDao.addid(s,rid);
        }
        PrintWriter out = resp.getWriter();
        out.print("1");
    }
    //查询权限
    private void  queryid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rid = req.getParameter("rid");
        List<Map> m=roleDao.queryid(rid);
        String map= JSON.toJSONString(m);
        PrintWriter out = resp.getWriter();
        out.print(map);

    }

    private void upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Map map=roleDao.findGustById(id);
        req.setAttribute("upd",map);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String gid = req.getParameter("id");
        System.out.println(gid);
        //dao   删除
        int i= roleDao.delGust(gid);
        if(i>0){
            //
            resp.sendRedirect("user/showRole.jsp");
        }
    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Role g=new Role();
        TransBean.populate(g,parameterMap);
        int i=roleDao.add(g);
        //
        resp.sendRedirect("queryAllRole");
    }
}
